/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.game.engine;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.util.Log;
import android.util.DisplayMetrics;


/**
 * TileView: a View-variant designed for handling arrays of "icons" or other
 * drawables.
 * 
 */
public class TileView extends View {

    /**
     * Parameters controlling the size of the tiles and their range within view.
     * Width/Height are in pixels, and Drawables will be scaled to fit to these
     * dimensions. X/Y Tile Counts are the number of tiles that will be drawn.
     */

    protected static int mTileSize;

    protected static int mXTileCount;
    protected static int mYTileCount;

    private static int mXOffset;
    private static int mYOffset;
    
    public int dis_x = 0;
    public int dis_y = 0;
    public int d_col = 0;
    public int d_row = 0;
    public int ismoving = 0;
    


    /**
     * A hash that maps integer handles specified by the subclasser to the
     * drawable that will be used for that reference
     */
    private Bitmap[] mTileArray; 

    /**
     * A two-dimensional array of integers in which the number represents the
     * index of the tile that should be drawn at that locations
     */
    private int[][] mTileGrid;

    private final Paint mPaint = new Paint();

	private Object Rect;

    public TileView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        initTileView(4, 4);
    }

    public TileView(Context context, AttributeSet attrs) {
        super(context, attrs);
       
        initTileView(4, 4);
    }

    void initTileView(int xTileCount, int yTileCount)
    {
        mXTileCount = xTileCount;
        mYTileCount = yTileCount;

        mTileSize = 75;
        
        mXOffset = 1;
        mYOffset = 1;

        mTileGrid = new int[mXTileCount][mYTileCount];
        clearTiles();
        
        Log.i("initTileView","mTileSize"+mTileSize);
        
        int width,height;
        DisplayMetrics dm = getResources().getDisplayMetrics();  
    	
    	width = dm.widthPixels;
    	height = dm.heightPixels;
    	if(width<height)
    		mTileSize = width/4;
    	else
    		mTileSize = height/4;
    	
    	Log.i("initTileView","dm.widthPixels++++++++++++"+width);
    	Log.i("initTileView","dm.heightPixels+++++++++++"+height);
    	
    	
    	Log.i("initTileView","mTileSize+++++++++++++++++"+mTileSize);
    }
    
    /**
     * Rests the internal array of Bitmaps used for drawing tiles, and
     * sets the maximum index of tiles to be inserted
     * 
     * @param tilecount
     */
    
    public void resetTiles(int tilecount) {
    	mTileArray = new Bitmap[tilecount];
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	
    	//mTileSize = w / mXTileCount;
    	//temp = h / mYTileCount;
    	//if(mTileSize > temp) mTileSize=temp;
    	//if(mTileSize > 96)mTileSize=96;
    	/*
    	int width,height;
    	DisplayMetrics dm = getResources().getDisplayMetrics();  
    	
    	width = dm.widthPixels;
    	height = dm.heightPixels;
    	if(w<h)
    		mTileSize = width/4;
    	else
    		mTileSize = height/4;
    	
    	Log.i("onSizeChanged","dm.widthPixels++++++++++++"+width);
    	Log.i("onSizeChanged","dm.heightPixels+++++++++++"+height);
    	
    	
    	Log.i("onSizeChanged","mTileSize+++++++++++++++++"+mTileSize);
    	
    	Log.i("onSizeChanged","w++++++++++++++++++++++++++++++++"+w);
    	Log.i("onSizeChanged","h++++++++++++++++++++++++++++++++"+h);
    	Log.i("onSizeChanged","oldw+++++++++++++++++++++++++++++"+oldw);
    	Log.i("onSizeChanged","oldh+++++++++++++++++++++++++++++"+oldh);
    	*/
//        mXTileCount = (int) Math.floor(w / mTileSize);
//        mYTileCount = (int) Math.floor(h / mTileSize);

//        mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
//        mYOffset = ((h - (mTileSize * mYTileCount)) / 2);

//        mTileGrid = new int[mXTileCount][mYTileCount];
//        clearTiles();
    }

    /**
     * Function to set the specified Drawable as the tile for a particular
     * integer key.
     * 
     * @param key
     * @param tile
     */
    public void loadTile(int key, Drawable tile) {

//    	mTileSize = getWidth()/mXTileCount;

    	if(key>0)
    	{
    	Bitmap bitmap = Bitmap.createBitmap(mTileSize*2/3, mTileSize*2/3, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        tile.setBounds(0, 0, mTileSize*2/3, mTileSize*2/3);
        tile.draw(canvas);
        
        mTileArray[key] = bitmap;
    	}
    	else if(key==0)
    	{
        	Bitmap bitmap = Bitmap.createBitmap(mTileSize*4, mTileSize*4, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            tile.setBounds(0, 0, mTileSize*4, mTileSize*4);
            tile.draw(canvas);
            
            mTileArray[key] = bitmap;
    	}
        
        Log.i("loadTile","mTileSize"+mTileSize);
    }

    /**
     * Resets all tiles to 0 (empty)
     * 
     */
    public void clearTiles() {
        for (int x = 0; x < mXTileCount; x++) {
            for (int y = 0; y < mYTileCount; y++) {
                setTile(0, x, y);
            }
        }
    }

    /**
     * Used to indicate that a particular tile (set with loadTile and referenced
     * by an integer) should be drawn at the given x/y coordinates during the
     * next invalidate/draw cycle.
     * 
     * @param tileindex
     * @param x
     * @param y
     */
    public void setTile(int tileindex, int x, int y) {
        mTileGrid[x][y] = tileindex;
    }
    
    public int GetmTileSize()
    {
    	return mTileSize;
    }

    public void Getmoving(int d_x, int d_y, int start_col, int start_row, int moving)
    {
    	dis_x = d_x;
    	dis_y = d_y;
    	d_col = start_col;
    	d_row = start_row;
    	ismoving = moving;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mTileArray[0],mXOffset,mYOffset,mPaint);
        if(ismoving ==0)
        {
	        for (int x = 0; x < mXTileCount; x += 1) {
	            for (int y = 0; y < mYTileCount; y += 1) {
	                if (mTileGrid[x][y] > 0) {
	                    canvas.drawBitmap(mTileArray[mTileGrid[x][y]], 
	                    		mXOffset + x * mTileSize + mTileSize/6,
	                    		mYOffset + y * mTileSize + mTileSize/6,
	                    		mPaint);
	                }
	            }
	        }
        }
        else
        {
        	for (int x = 0; x < mXTileCount; x += 1) {
	            for (int y = 0; y < mYTileCount; y += 1) {
	            	if(x!=d_col || y!=d_row)
	            	{
		                if (mTileGrid[x][y] > 0) {
		                    canvas.drawBitmap(mTileArray[mTileGrid[x][y]], 
		                    		mXOffset + x * mTileSize + mTileSize/6,
		                    		mYOffset + y * mTileSize + mTileSize/6,
		                    		mPaint);
		                }
	            	}
	            }
	        }
        	
        	if (mTileGrid[d_col][d_row] > 0) {
                canvas.drawBitmap(mTileArray[mTileGrid[d_col][d_row]], 
                		mXOffset + d_col * mTileSize + mTileSize/6 + dis_x,
                		mYOffset + d_row * mTileSize + mTileSize/6 + dis_y,
                		mPaint);
        	}
        }
        Log.i("onDraw","mTileSize"+mTileSize);
    }

}
